package com.example.course

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class SeeCourseActivity : AppCompatActivity() {

    private val databaseHelper by lazy { DatabaseHelper(this, "database.db", null, 1) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_see_course)
        val intent = intent
        val seeCourse = intent.getSerializableExtra("seeCourse") as Course
        val seeCourseName = findViewById<TextView>(R.id.see_course_name)
        val seeDay = findViewById<TextView>(R.id.see_week)
        val seeStart = findViewById<TextView>(R.id.see_classes_begin)
        val seeEnd = findViewById<TextView>(R.id.see_classes_ends)
        val seeTeacher = findViewById<TextView>(R.id.see_teacher_name)
        val seeClassRoom = findViewById<TextView>(R.id.see_class_room)
        seeCourseName.text = seeCourse.courseName
        seeDay.text = seeCourse.day.toString()
        seeStart.text = seeCourse.classStart.toString()
        seeEnd.text = seeCourse.classEnd.toString()
        seeTeacher.text = seeCourse.teacher
        seeClassRoom.text = seeCourse.classRoom

        val delBtn: Button = findViewById(R.id.btn_del)
        val reviseBtn: Button = findViewById(R.id.btn_revise) // Ensure reviseBtn is defined

        delBtn.setOnClickListener {
            val sqLiteDatabase = databaseHelper.writableDatabase
            sqLiteDatabase.execSQL(
                "DELETE FROM courses WHERE course_name = ? AND day = ? AND class_start = ? AND class_end = ?",
                arrayOf(seeCourse.courseName, seeCourse.day.toString(), seeCourse.classStart.toString(), seeCourse.classEnd.toString())
            )
            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show()
            val resultIntent = Intent()
            resultIntent.putExtra("isDelete", true)
            resultIntent.putExtra("PreCourse", seeCourse)
            setResult(Activity.RESULT_OK, resultIntent)
            finish()
        }

        reviseBtn.setOnClickListener {
            val intent = Intent(this, AddCourseActivity::class.java)
            intent.putExtra("ReviseCourse", seeCourse)
            intent.putExtra("isRevise", true)
            startActivityForResult(intent, 2)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 2 && resultCode == Activity.RESULT_OK && data != null) {
            val newCourse = data.getSerializableExtra("newCourse") as Course
            val preCourse = data.getSerializableExtra("PreCourse") as Course
            val sqLiteDatabase = databaseHelper.writableDatabase
            sqLiteDatabase.execSQL(
                "UPDATE courses SET course_name = ?, teacher = ?, class_room = ?, day = ?, class_start = ?, class_end = ? " +
                        "WHERE course_name = ? AND day = ? AND class_start = ? AND class_end = ?",
                arrayOf(
                    newCourse.courseName, newCourse.teacher, newCourse.classRoom, newCourse.day.toString(),
                    newCourse.classStart.toString(), newCourse.classEnd.toString(),
                    preCourse.courseName, preCourse.day.toString(), preCourse.classStart.toString(), preCourse.classEnd.toString()
                )
            )
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show()
            val resultIntent = Intent()
            resultIntent.putExtra("isDelete", false)
            resultIntent.putExtra("PreCourse", preCourse)
            resultIntent.putExtra("newCourse", newCourse)
            setResult(Activity.RESULT_OK, resultIntent)
            finish()
        }
    }
}